<?php
class _admin extends BaseAdmin
{
	public function initc()
	{
	    View::preshow('public/discuss/admin_header');
	    View::endshow('public/discuss/footer');
	}
    
    /**
     * 话题, 审核列表
     */
    public function topicReviewList()
    {
        View::$arrTplVar['list'] = Discuss::link('opinion')
            ->where(['status' => Discuss::STATUS_INIT])
            ->select()
            ->getAll();
        View::show('topic_check_list');
    }
    
    /**
     * 回复, 审核列表
     */
    public function replayReviewList()
    {
        View::$arrTplVar['list'] = Discuss::link('review')
            ->where(['status' => Discuss::STATUS_REPLAY_INIT])
            ->select()
            ->getAll();
        View::show('replay_check_list');
    }
    
    /**
     * 话题审核通过
     */
    public function topicok()
    {
        $id = Request::Post('id', 0, Safe::$Check_Empty);
        Discuss::link('opinion')
            ->where(['id' => $id])
            ->updateVal(['status' => Discuss::STATUS_OK])
            ->update()
            ->query();
        Response::success();
    }
    
    /**
     * 话题, 未通过审核
     */
    public function topicdel()
    {
        $id = Request::Post('id', 0, Safe::$Check_Empty);
        Discuss::link('opinion')
            ->where(['id' => $id])
            ->updateVal(['status' => Discuss::STATUS_DEL])
            ->update()
            ->query();
        Response::success();
    }
    
    /**
     * 回复, 审核通过, 添加到回复表中
     */
    public function replayok()
    {
        //====获取审核成功的信息
        $reviewId = Request::Post('id');
        
        //更新状态
        Discuss::link('review')
            ->where(['id' => $reviewId])
            ->updateVal(['status' => Discuss::STATUS_REPLAY_OK])
            ->update()
            ->query();
    
        //获取回复信息
        $review = Discuss::link('review')
            ->where(['id' => $reviewId])
            ->getOne();
        $rootId   = $review['rootid'];
        $fatherId = $review['fatherid'];
        $content  = $review['content'];
        
        //===插入到replay表
        if (empty($fatherId)) { //一级回帖
            Discuss::link('replay')
                ->insert([
                    'id'       => $reviewId,
                    'rootid'   => $rootId,
                    'fatherid' => 0,
                    'path'     => $reviewId,
                    'len'      => 1,
                    'content'  => $content
                ])->query();
            
        } else { //非一级回帖
            $fatherInfo = Discuss::link('replay')
                ->where(['id' => $fatherId])
                ->getOne();
    
            $fatherPath = $fatherInfo['path'];
            $len = $fatherInfo['path'] + 1; //纪录层级
    
            Discuss::link('replay')
                ->insert([
                    'id'       => $reviewId,
                    'rootid'   => $rootId,
                    'fatherid' => $fatherId,
                    'path'     => $fatherPath . ',' . $reviewId,
                    'len'      => $len,
                    'content'  => $content
                ])
                ->query();
        }
        
        Response::success();
    }
    
    /**
     * 删除回复
     */
    public function replaydel()
    {
        $id = Request::Post('id', 0, Safe::$Check_Empty);
        Discuss::link('review')
            ->where(['id' => $id])
            ->updateVal(['status' => Discuss::STATUS_REPLAY_DEL])
            ->update()
            ->query();
        Response::success();
    }
    
}